package com.project.slsx.adapter;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.project.slsx.Constant.Constans;
import com.project.slsx.R;
import com.project.slsx.activity.OrderListActivity;
import com.project.slsx.bean.OrderListBean;
import com.project.slsx.bean.ResultBean;
import com.project.slsx.utils.ParamsUtils;
import com.project.slsx.utils.TimeUtils;

public class OrderListAdapter extends BaseQuickAdapter<OrderListBean.ResultBean, BaseViewHolder> {
    public OrderListAdapter(int layoutResId) {
        super(layoutResId);
    }

    @Override
    protected void convert(BaseViewHolder holder, OrderListBean.ResultBean item) {
        RecyclerView rv2 = holder.getView(R.id.recyclerview2);
        rv2.setLayoutManager(new LinearLayoutManager(mContext));
        OrderList2Adapter adapter2 = new OrderList2Adapter(R.layout.layout_item_order_list2);
        View footer = LayoutInflater.from(mContext).inflate(R.layout.layout_item_order_list2_footer, null);
        adapter2.setFooterView(footer);
        rv2.setAdapter(adapter2);
        adapter2.setNewData(item.getGoods());

        holder.setText(R.id.tv_order_no, item.getOrdersn());
        holder.setText(R.id.tv_order_time, item.getAddtime());
        holder.setText(R.id.tv_order_pay, item.getPay_type());
        TextView goodsNum = footer.findViewById(R.id.tv_goods_num);
        TextView sumPrice = footer.findViewById(R.id.tv_goods_sum_price);
        goodsNum.setText(item.getNum());
        sumPrice.setText(item.getPrice());

        //再来一单
        footer.findViewById(R.id.btn_order_more).setOnClickListener(v -> {

        });

        //重新支付
        Button btnPay = footer.findViewById(R.id.btn_order_repay);
        btnPay.setText(setPayText(item));
        btnPay.setOnClickListener(v -> {
            doPap(item);
        });
    }

    /**
     * 设置支付按钮文字
     *
     * @param item
     * @return
     */

    private String setPayText(OrderListBean.ResultBean item) {
        String payText = "";
        if (item.getIs_pay().equals("1")) {
            payText = "已经支付";
        } else if (item.getIs_pay().equals("0")) {
            payText = "去支付";
        }
        return payText;
    }

    /**
     * 支付
     *
     * @param order
     */
    private void doPap(OrderListBean.ResultBean order) {
        HttpParams orderPayParams = ParamsUtils.getOrderPayParams();
        orderPayParams.put("order_id", order.getId());
        orderPayParams.put("type", getPayType(order.getPay_type()));
        orderPayParams.put("code", "");
        orderPayParams.put("mobile", "");
        orderPayParams.put("password", "");
        OkGo.<String>post(Constans.BASEURL).tag("orderpay").params(orderPayParams).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                Log.i(TAG, "onSuccess: " + response.body());
                ResultBean resultBean = JSONObject.parseObject(response.body(), ResultBean.class);
                ((OrderListActivity) mContext).showToast(mContext, resultBean.getErrmsg());
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                Log.i(TAG, "onError: ");
            }

            @Override
            public void onFinish() {
                super.onFinish();

            }
        });
    }

    private String getPayType(String pay_type) {
        String payType = "";
        if (pay_type.equals("现金支付")) {
            payType = "1";
        } else if (pay_type.equals("会员卡支付")) {
            payType = "6";
        }
        return payType;
    }
}
